/*
 * Copyright (C) 2008, Velocityme Partnership
 * ShowDefaultAction.java
 *
 * Created on 30 August 2008, 21:10
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.Node;
import com.velocityme.entity.Sprint;
import com.velocityme.session.KeyLocal;
import com.velocityme.session.NodeLocal;
import com.velocityme.www.actionforms.NodeIdActionForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ShowBurnDownAction extends org.apache.struts.action.Action {
    
    /** Set nodePK in request */
    @Override
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        NodeIdActionForm nodeAF = (NodeIdActionForm)form;
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal) session.getAttribute("sessionKey");
        if (nodeAF.getNodeId() == 0)
            nodeAF.setNodeId(((Long)session.getAttribute("nodeId")));
        Long nodeId = nodeAF.getNodeId();
        request.setAttribute("nodeId", nodeId);
        Node node = lookupNodeBean().findNode(key, nodeId);
        if (node instanceof Sprint) {
            return mapping.findForward("ACTION_SPRINT_SUCCESS");
        }
        return mapping.findForward("ACTION_TASK_SUCCESS");
    }

    private NodeLocal lookupNodeBean() {
        try {
            Context c = new InitialContext();
            return (NodeLocal) c.lookup("java:comp/env/NodeBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
        
}
